/* 
 * @Author: jade
 * @Date:   2015-12-21 10:33:08
 * @Last Modified by:   jade
 * @Last Modified time: 2016-01-19 18:45:47
 */
var Crawler = require('crawler');
var fs = require('fs');
var path = require('path');
var argv = require('yargs').argv;
var city = argv.city;
var xq = argv.xq;
var json = !!argv.json;
var page = ~~argv.page;

//上海康城四期 5011000012332 http://sh.lianjia.com/xiaoqu/5011000012332/esf/pg1co32/
//世茂滨江花园 5011000017872 http://sh.lianjia.com/xiaoqu/5011000017872/esf/co32/
//贝港南区    5011000012143  http://sh.lianjia.com/xiaoqu/5011000012143/esf/co32/
//解放新村 5011000014838 http://sh.lianjia.com/xiaoqu/5011000014838/esf/
//卓越世纪中心 5011000009003 http://sh.lianjia.com/xiaoqu/5011000009003/esf/
try {
    var xqJson = require('./zuixin/' + xq + '.js');
} catch (e) {

}
//node lianjia.js --city sh --xq --json --page 11
//sh:上海，
var rootPath = 'http://' + city + '.lianjia.com';

function store(str, fileName) {
    fs.appendFile(fileName || './zuixin/' + xq + '.txt', str + '\n', function(err) {
        err && console.log(err)
    });
}

function storeJSON(str, fileName) {
    fs.appendFile(fileName || './zuixin/' + xq + '.js', str + '\n', function(err) {
        err && console.log(err)
    });
}


function getRandom() {
    return ~~(Math.random() * 10) + 0.1
}

function processStr(str) {
    return str.replace(/,/g, '，').replace(/\n/g, '');
}

// if (page == 1) {
//     var tilte = 'IW小区ID,区域板块,小区名,户型,面积（单位：平米）,楼层,签约日期,成交价,成交单价'
//     store(tilte);
// }

function addQ(url, timer, cb, text) {
    (function(url, timer, cb, text) {
        setTimeout(function() {
            //console.log(text);
            c.queue(url);
            cb && cb();
        }, timer)
    })(url, timer, cb, text)
}

var index = 0;
var c = new Crawler({
    maxConnections: 200,
    //skipDuplicates: true,
    // This will be called for each crawled page
    callback: function(error, result, $) {
        $('#house-lst li h2 a').each(function(itemIndex, item) {
            var href = $(this).attr('href');
            var s = href.split('/')
            var id = s[s.length - 1].split('.')[0];
            console.log(++index + '=' + id);
            if (json) {
                storeJSON('"' + id + '": "' + href + '",');
            } else {
                if (!xqJson[id]) {
                    store(id + '=' + href);
                }
            }
        })
    }
});

function start() {
    for (var i = 0; i < page; i++) {
        var timer = 1000 * i;
        var url = rootPath + '/xiaoqu/' + xq + '/esf/pg' + (i + 1) + 'co32/';

        addQ(url, timer, function() {}, '正在爬第' + (i + 1));
    }
}
if (json) {
    start();
} else {
    setInterval(function() {
        start();
    }, 1000 * 60);
}
